package com.pgcomb.poweriot.alarmservice.alarm.service;

import com.pgcomb.common.bean.AlarmData;
import com.pgcomb.common.bean.AlarmParam;
import com.pgcomb.common.enums.AlarmParamType;
import com.pgcomb.common.enums.AlarmType;
import com.pgcomb.common.enums.GlobalCodeEnum;
import com.pgcomb.poweriot.alarmservice.alarm.AlarmMessageFactory;
import com.pgcomb.poweriot.alarmservice.alarm.CommonAlarmMessage;
import com.pgcomb.poweriot.alarmservice.global.ErrorCodeException;
import com.pgcomb.web.dao.Equipment;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhengheqi
 * @version 1.0
 * @description: 设备数据参数异常 告警信息
 * @date 2021/5/29 16:00
 */
@Component
public class DataAlarmMessage extends CommonAlarmMessage implements InitializingBean {

    @Override
    public String getAlarmMessage(AlarmData alarmData) throws Exception{
        String sbId = alarmData.getSbId();
        Equipment equipment = getEquipment(sbId);
        if(ObjectUtils.isEmpty(equipment)){
            throw new ErrorCodeException(GlobalCodeEnum.MISSING_DB.getCode(),"Equipment is not exist sbId:"+sbId);
        }

        //TODO 组装告警信息
        StringBuilder alarmMessage = new StringBuilder();
        List<AlarmParam> param = alarmData.getParam();
        for (AlarmParam alarmParam : param) {

            String name = alarmParam.getName();//设备 参数名称
            Integer threshold = alarmParam.getThreshold();//阈值
            String type = alarmParam.getType();
            Integer value = alarmParam.getValue();

            AlarmParamType param1 = AlarmParamType.getParam(type);
            String paramType = type;
            if(ObjectUtils.isNotEmpty(param1)){
                paramType = param1.getValue();
            }

            alarmMessage.append(name).append("当前值为：").append(value).append(",").append(paramType).append("阈值").append(threshold);
            alarmMessage.append(";");
        }


        return alarmMessage.toString();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmMessageFactory.register(AlarmType.DATA_ALARM, this);
    }
}
